﻿#include "addfriendlistwidget.h"
#include "ui_addfriendlistwidget.h"
#include<QDebug>

AddFriendListWidget::AddFriendListWidget(unordered_map<string,string>* newFriendPtr,SOCKET sockCli,string sender,QWidget *parent) :
    QWidget(parent),
    newFriendPtr(newFriendPtr),
    sockCli(sockCli),
    sender(sender),
    ui(new Ui::AddFriendListWidget)
{
    ui->setupUi(this);
    qDebug()<<newFriendPtr->size();
    setAttribute(Qt::WA_DeleteOnClose, true);
    //增加按钮和槽函数
    //char* firendListptr = new char[1000];
    qDebug()<<u8"AddFriendListWidget::showNewFirendList";
    //遍历newFriendPtr 下的  信息
    for(int i = 0;i<MAX_FIREND_NUM;i++) isopen[i] = false;
    btnNum = 0;
    for(auto each:*newFriendPtr){
        QString friendName = u8"新朋友"+ QString::fromStdString(each.first);
        qtbs[btnNum] = new QPushButton(this);//增加按钮
        qtbs[btnNum]->setText(friendName);//增加好友名称
        qtbs[btnNum]->setFixedSize(200,50);
        ui->newFriendList->addWidget(qtbs[btnNum]);
        btnNum++;
    }
    int i = 0;
    for(auto each:*newFriendPtr){
        connect(qtbs[i],&QPushButton::clicked,this,[=](){
            if(isopen[i] == false){
                AcceptFriend* af = new AcceptFriend(each.first,newFriendPtr,&isopen[i],qtbs[i],sockCli,sender);
                af->show();
            }
        });
        i++;
    }
}

AddFriendListWidget::~AddFriendListWidget()
{
    //析构按钮
    for(int i = 0;i<btnNum;i++){
        delete qtbs[i];
    }
    qDebug()<<u8"AddFriendListWidget析构";
    delete ui;
}
